#!/usr/bin/perl -w
# WARCARRIER (c) GNU - 2013 WeakNet Labs
#
# "...because when we are done hacking the planet,
#   - we will hack the sky!" - Douglas Berdeaux 2013
#
# Coded by Douglas Berdeaux
# WeakNetLabs@Gmail.com
#
# Please see README for GNU License.
# 
# NOTES: any hacking to this file CAN be bad. curses in Perl
#   is very delicate and will fail without returning errors
#   if teh slightest thing goes wrong. In fact, if it does it
#   without hacking, just restart it because it probabaly means
#   that the buffer files haven't been made yet.
#   If you see any "sleep"'s in here - that's why.
#
# CLEANUP: please run "./wcclean proc" to destroy any left processes
#   by Curses::UI failing before stopping them. This won't destory
#   the log files, to do that do: "./wcclean logs" or "./wcclean all"
# 
# UBERTOOTH: this functionality takes the "raw" daya from the Ubertooth
#   device using spectools_raw from Dragorn.
#
# My own modules:
require lib::GMAPS;   # - create "map" objects
require lib::ARGS;    # - get arguments nicely
require lib::GPSO;    # - get GPS data from gpsd
require lib::WIFI;    # - get wifi data from Airodump-ng output csv file
require lib::SATINFO; # - get satellite info from NASA website (offline)
use warnings;         # obviously.
use strict;
# Other modules
use Curses::UI; # See CPAN website for usage on these
use JSON::XS;   #   modules.
### GET ARGUMENTS BEFORE CONTINUING ###
my $airchk = `aircrack-ng 2>/dev/null | grep Otre`; # grep Thomas
die "Please install the Aircrack-NG suite for use with Airodump-NG.\n" if ($airchk !~ m/Thomas/);
my $gpschk = `gpsd -V`;
die "Please install GPSd Version 3.2 before continuing\n" if ($gpschk !~ m/revision/);
my $filename; # to read from
my $wfdev; # wifi device
my $btdev = ''; # bluetooth device
my $date = localtime();
my $sats;
my $wpc = 0; # waypoints counter
my $btdc = 0;

my $btdisp = ''; # for showing local Bluetooth devices
my @bttds; # for logging ALL found Bluetooth devices
### MY WAYPOINTS AND MAP STUFF ###
my @wps;
if(!$ARGV[0]){
	usage(1);
}else{ # create args object and pull what we need:
	my $args = lib::ARGS->new(@ARGV);
	$wfdev = $args->getarg('-d',1); # pass getarg('flag','boolean (for required));
	$filename = "logs/capfiles/" . $args->getarg('-f',1) . localtime();
	$filename =~ s/ /./g;
}
my $devchk = `iwconfig $wfdev`; # let's see if they passed a valid device,
if($devchk eq ''){              #  so we can fail gracefully if so.
	usage('dev');
}
my $airtest = `ps aux | grep "airodump.*[c]apfiles" |wc -l`;
if($airtest < 1){ # just in case Curses::UI - well, you know.
	system("airodump-ng $wfdev --write $filename 2>/dev/null &"); # start scanning wifi
}else{
	$filename = `ps aux | grep -i airodump-n[g] | head -n 1 | awk '{print \$14}'`;
	$filename.="-01.csv"; # restart pipe
}
$filename.="-01.csv"; # Airodump-ng does this to make unique files
system("touch bt.out"); # create our bluetooth scanning file
system("touch ubt.out"); # create our bluetooth spec-an file
system("touch gps.TPV"); # create our gps TPV Packet file
system("touch gps.SKY"); # create our gps SKY packet file

my $cui = new Curses::UI(-color_support=>1); # create a curses object
$cui->status(-message=>"Please wait while the WARCARRIER initializes",-fg=>"white",-bold=>1,-bg=>"black"); # please hold.

my @allsats; # for ALL satellites (HTML logging)
my $gps = "NULL";
my $lat = "0.00000N";
my $lon = "0.00000W";
my $alt = 0;
my $dump = ''; # dump string for printing in window
my $apc = 0; # AP Count for WiFi
my %jhash = ();
my $init = 1; # slow down the first read
# These are for graphing WiFi
my @lines = ();
clrgraph();
my $act = 0;
# these are totals:
my $wpac = 0;
my $wpa2c = 0;
my $wepc = 0;
my $opnc = 0;
my @logaps; # LOG of all APs and their Lat, Lon, Alt.
my $catchme= ""; # Catchme-NG!
my $catch = 0; # token to search
my $satsq = ""; # this is a temporary string (list of satellites)

$gps = lib::GPSO->new(); # create a new GPSO Object;

### WINDOW DRAWING ###
local $winlat::label;  # these are local variables that I can cal;l from sub
local $winlon::label;  #   routines and rewrite the god damn it fuick im furcking ddrruink.
local $winalt::label;  
local $wincli::label;  
local $winspe::label;  
local $wintra::label;  
local $winsat::label;  
local $winson::label;  
local $winsc::label;  
local $winfix::progressbar;

### These are channel bars ###
local $windmp::label;
local $winch11::label;
local $winch10::label;
local $winch9::label;
local $winch8::label;
local $winch7::label;
local $winch6::label;
local $winch5::label;
local $winch4::label;
local $winch3::label;
local $winch2::label;
local $winch1::label;
local $winch12::label;
local $winch13::label;

### THESE ARE LABELS FOR THE UBERTOOTH RSSIs ###
local $ubt2408::label;
local $ubt2409::label;
local $ubt2410::label; 
local $ubt2411::label; 
local $ubt2412::label; 
local $ubt2413::label; 
local $ubt2414::label; 
local $ubt2415::label; 
my @arrlabel; # retarded
local $ubt2416::label; 
local $ubt2417::label; 
local $ubt2418::label; 
local $ubt2419::label; 
local $ubt2420::label; 
local $ubt2421::label; 
local $ubt2422::label; 
local $ubt2423::label; 
local $ubt2424::label; 
local $ubt2425::label; 
local $ubt2426::label; 
local $ubt2427::label; 
local $ubt2428::label; 
local $ubt2429::label; 
local $ubt2430::label; 
local $ubt2431::label; 
local $ubt2432::label; 
local $ubt2433::label; 
local $ubt2434::label; 
local $ubt2435::label; 
local $ubt2436::label; 
local $ubt2437::label; 
local $ubt2438::label; 
local $ubt2439::label; 
local $ubt2440::label; 
local $ubt2441::label; 
local $ubt2442::label; 
local $ubt2443::label; 
local $ubt2444::label; 
local $ubt2445::label; 
local $ubt2446::label; 
local $ubt2447::label; 
local $ubt2448::label; 
local $ubt2449::label; 
local $ubt2450::label; 
local $ubt2451::label; 
local $ubt2452::label; 
local $ubt2453::label; 
local $ubt2454::label; 
local $ubt2455::label; 
local $ubt2456::label; 
local $ubt2457::label; 
local $ubt2458::label; 
local $ubt2459::label; 
local $ubt2460::label; 
local $ubt2461::label; 
local $ubt2462::label; 
local $ubt2463::label; 
local $ubt2464::label; 
local $ubt2465::label; 
local $ubt2466::label; 
local $ubt2467::label; 
local $ubt2468::label; 
local $ubt2469::label; 
local $ubt2470::label; 
local $ubt2471::label; 
local $ubt2472::label; 
=pod
local $ubt2473::label; 
local $ubt2474::label; 
local $ubt2475::label; 
local $ubt2476::label; 
local $ubt2477::label; 
local $ubt2478::label; 
local $ubt2479::label; 
local $ubt2480::label; 
=cut
### UBERTOOTH RECORDING ###
local $rec::label; 
my $record = 0; # boolean for recording
my $recblink = 0; # blink 0 = off # 1 = on
my $f = 0;
my $udisp = 0; # Ubertooth screen displaying?

my $winubt;
my $ubton = 0;

local $wingrph::label; # The 802.11 graph!

local $winwpa2::label; # All WPA2 count
local $winwpa::label; # WPA count
local $winwep::label; # All WEP count
local $winopn::label; # All OPN count
local $winapc::label; # all AP count

local $winclock::label; # the clock in the middle of the app

local $winbt::label; # The Bluetooth dump window!

my $titlewid = $cui->add('titlew','Window',-border=>0,-height=>1,-width=>34);
$titlewid->add('titlel', 'Label',-text =>" WARCARRIER - 2013 WeakNet Labs",-bold => 1,-fg=>"blue",-height=>1);
my $widlon = $cui->add('lon', 'Window',-border => 1,-title=>'Longitude',-height=>1,-width=>15,-y=>2);
my $widlat = $cui->add('lat', 'Window',-border => 1,-title=>'Latitude',-height=>1,-width=>15,-y=>5);
my $widalt = $cui->add('alt', 'Window',-border => 1,-title=>'Altitude',-height=>1,-width=>15,-y=>8);
my $widcli = $cui->add('cli', 'Window',-border => 1,-title=>'Climb',-height=>1,-width=>15,-y=>11);
my $widspe = $cui->add('spe', 'Window',-border => 1,-title=>'Speed',-height=>1,-width=>15,-y=>14);
my $widtra = $cui->add('tra', 'Window',-border => 1,-title=>'Track',-height=>1,-width=>15,-y=>14,-x=>15);
my $widsc = $cui->add('satcount','Window',-border=>1,-title=>'Total',-height=>1,-y=>14,-x=>30,-width=>15);
my $widso = $cui->add('saton','Window',-border=>1,-title=>'Online',-height=>1,-y=>14,-x=>45,-width=>14);

## CLOCK:
my $widclock = $cui->add('clock','Window',-border=>0,-height=>1,-x=>35,-width=>25);
$winclock::label = $widclock->add('wclock','Label',-text=>$date,-bold=>0,height=>1,-width=>25);
$winclock::label->draw();

## SATELLITE BEEF:
my $widsat = $cui->add('sat','Window',-border=>1,-tbg=>"yellow",-tfg=>"black",-title=>'Satellites',-height=>12,-x=>15,-y=>2,-width=>44);
my $widfix = $cui->add('fix','Window',-border=>1,-title=>'Fix Type',-y=>17,-width=>47,-height=>5,-fg=>"red");

## LABELS, PROG BARS, ETC:
$winlon::label = $widlon->add('lon', 'Label',-text =>"Retrieving Data",-bold => 0,-width=>100,-height=>1);
$winlon::label->draw;
$winlat::label = $widlat->add('lat', 'Label',-text =>"Retrieving Data",-bold => 0,-width=>100,-height=>1);
$winlat::label->draw;
$winalt::label = $widalt->add('alt', 'Label',-text =>"Retrieving Data",-bold => 0,-width=>100,-height=>1);
$winalt::label->draw;
$wincli::label = $widcli->add('cli', 'Label',-text =>"Retrieving Data",-bold => 0,-width=>100,-height=>1);
$wincli::label->draw;
$wintra::label = $widtra->add('tra', 'Label',-text =>"Retrieving Data",-bold => 0,-width=>100,-height=>1);
$wintra::label->draw;
$winspe::label = $widspe->add('spe', 'Label',-text =>"Retrieving Data",-bold => 0,-width=>100,-height=>1);
$winspe::label->draw;
$winsc::label = $widsc->add('satcount', 'Label',-text =>"Retrieving Data",-bold => 0,-width=>100,-height=>1);
$winsc::label->draw;
$winson::label = $widso->add('saton', 'Label',-text =>"Retrieving Data",-bold => 0,-width=>100,-height=>1);
$winson::label->draw;
$winfix::progressbar = $widfix->add('myfix','Progressbar',-max=>12,-min=>0,-pos=>0);
$winfix::progressbar->draw;
$winsat::label = $widsat->add('sat', 'Label',-text =>"Retrieving Data",-bold => 0,-width=>100,-height=>22);
$winsat::label->draw;


## WIFI WINDOWS:
my $widdmp = $cui->add('wifidmp','Window',-border=>1,-tbg=>"yellow",-tfg=>"black",-title=>'Collected WiFi Data',-height=>15,-x=>59,-y=>2,-width=>58);

my $widch1 = $cui->add('ch1','Window',-border=>1,-title=>'01',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>47,-titlefullwidth=>1);
$winch1::label = $widch1->add('ch1l','Label',-height=>10,-width=>7);
$winch1::label->draw();
my $widch2 = $cui->add('ch2','Window',-border=>1,-title=>'02',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>53,-titlefullwidth=>1);
$winch2::label = $widch2->add('ch2l','Label',-height=>10,-width=>7);
$winch2::label->draw();
my $widch3 = $cui->add('ch3','Window',-border=>1,-title=>'03',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>59,-titlefullwidth=>1);
$winch3::label = $widch3->add('ch3l','Label',-height=>10,-width=>7);
$winch3::label->draw();
my $widch4 = $cui->add('ch4','Window',-border=>1,-title=>'04',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>65,-titlefullwidth=>1);
$winch4::label = $widch4->add('ch4l','Label',-height=>10,-width=>7);
$winch4::label->draw();
my $widch5 = $cui->add('ch5','Window',-border=>1,-title=>'05',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>71,-titlefullwidth=>1);
$winch5::label = $widch5->add('ch5l','Label',-height=>10,-width=>7);
$winch5::label->draw();
my $widch6 = $cui->add('ch6','Window',-border=>1,-title=>'06',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>77,-titlefullwidth=>1);
$winch6::label = $widch6->add('ch6l','Label',-height=>10,-width=>7);
$winch6::label->draw();
my $widch7 = $cui->add('ch7','Window',-border=>1,-title=>'07',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>83,-titlefullwidth=>1);
$winch7::label = $widch7->add('ch7l','Label',-height=>10,-width=>7);
$winch7::label->draw();
my $widch8 = $cui->add('ch8','Window',-border=>1,-title=>'08',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>89,-titlefullwidth=>1);
$winch8::label = $widch8->add('ch8l','Label',-height=>10,-width=>7);
$winch8::label->draw();
my $widch9 = $cui->add('ch9','Window',-border=>1,-title=>'09',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>95,-titlefullwidth=>1);
$winch9::label = $widch9->add('ch9l','Label',-height=>10,-width=>7);
$winch9::label->draw();
my $widch10 = $cui->add('ch10','Window',-border=>1,-title=>'10',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>101,-titlefullwidth=>1);
$winch10::label = $widch10->add('ch10l','Label',-height=>10,-width=>7);
$winch10::label->draw();
my $widch11 = $cui->add('ch11','Window',-border=>1,-title=>'11',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>107,-titlefullwidth=>1);
$winch11::label = $widch11->add('ch11l','Label',-width=>7,-height=>10);
$winch11::label->draw();
my $widch12 = $cui->add('ch12','Window',-border=>1,-title=>'12',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>113,-titlefullwidth=>1);
$winch12::label = $widch1->add('ch12l','Label',-height=>10,-width=>7);
$winch12::label->draw();
my $widch13 = $cui->add('ch13','Window',-border=>1,-title=>'13',-y=>17,-width=>6,-height=>10,-fg=>"red",-x=>119,-titlefullwidth=>1);
$winch13::label = $widch2->add('ch13l','Label',-height=>10,-width=>7);
$winch13::label->draw();

## GRAPHING WINDOW:
my $widgrph = $cui->add('grph','Window',-border=>1,-title=>'802.11 Activity Graph',-y=>27,-width=>125,-height=>9);
$wingrph::label = $widgrph->add('grphl','Label',-width=>"135",-bold=>1,-height=>"7");
$wingrph::label->draw;

## Stats WIndows on the left:

my $widwpa2 = $cui->add('wpa2', 'Window',-border => 1,-title=>'WPA2',-height=>1,-width=>8,-y=>2,-x=>117);
$winwpa2::label = $widwpa2->add('wpa2c','Label',-text=>$wpa2c,-width=>8);
$winwpa2::label->draw;
my $widwpa = $cui->add('wpa', 'Window',-border => 1,-title=>'WPA',-height=>1,-width=>8,-y=>5,-x=>117);
$winwpa::label = $widwpa->add('wpac','Label',-text=>$wpac,-width=>8);
$winwpa::label->draw;
my $widwep = $cui->add('wep', 'Window',-border => 1,-title=>'WEP',-height=>1,-width=>8,-y=>8,-x=>117);
$winwep::label = $widwep->add('wepc','Label',-text=>$wepc,-width=>8);
$winwep::label->draw;
my $widopn = $cui->add('opn', 'Window',-border => 1,-title=>'OPN',-height=>1,-width=>8,-y=>11,-x=>117);
$winopn::label = $widopn->add('opnc','Label',-text=>$opnc,-width=>8);
$winopn::label->draw;
my $widapc = $cui->add('apc', 'Window',-border => 1,-title=>'APs',-height=>1,-width=>8,-y=>14,-x=>117);
$winapc::label = $widapc->add('opnc','Label',-text=>$apc,-width=>8);
$winapc::label->draw;

## LABELS, PROG BARS, ETC
$windmp::label = $widdmp->add('dmp','Label',-text=>'Retrieving Data',-bold=>0,-width=>100,-height=>33);
$windmp::label->draw();

### BLUETOOTH WINDOWS:
my $widbt = $cui->add('btd','Window',-border=>1,-title=>'Nearby Bluetooth Devices',-y=>22,-width=>47,-height=>5);
$winbt::label = $widbt->add('winbt','Label',-text=>"No remote devices found.",-ipadleft=>1,-height=>5,-width=>100);
$winbt::label->draw();

my $cmds = $cui->add('cmds','Window',-border=>0,-y=>36,-height=>1,-padleft=>1);
$cmds->add('cmdl','Label',-bold=>1,-text=>" Commands: (q)uit, (s)napshot, clear (g)raph, satellite (i)nfo, (c)atchme-NG!, map (w)aypoint, bl(u)etooth spectrum",-reverse=>1);

### KEY BINDINGS ###
$cui->set_binding( \&clrgraph, "g"); # Clear Graph
$cui->set_binding( \&eggsit, "\cC"); # literally means CTRL+C (Eggsit)
$cui->set_binding( \&eggsit, "q"); # eggsit
$cui->set_binding( \&snapshot, "\cS"); # literally means CTRL+S (Snapshots)
$cui->set_binding( \&snapshot, "s"); # s (Snapshots)
$cui->set_binding( \&satinfo, "i"); # i Satellite infoz
$cui->set_binding( \&catchmeinput, "c"); # c Catchme-NG
$cui->set_binding( \&stopubt, "x"); # Stop Ubertooth Scanner
$cui->set_binding( \&startubt, "u"); # Start Ubertooth Scanner
$cui->set_binding( \&waypoint, "w"); # set a waypoint into Google Map
$cui->set_binding( \&rec, "r"); # record BT DUMP

### END WINDOWS ###
#my $n = '('; # uncomment these two for ultra speed! (gps cannot keep up really though.
#$cui->set_timer(1, \&getgpsd,$n); #

$cui->set_timer('update_timer',\&getgpsd,0); # will do this each second:
$cui->mainloop();

sub clock{
	$date = localtime();
	$winclock::label->text($date);
}
sub getgpsd{
	clock();
	my $w = `ps aux | grep [T]PV | wc -l`;
	if($w < 1){ 
		system("./wcd TPV &");
		system("./wcd SKY &");
		system("./wcd bt $lat $lon &");
	}else{
		# it's already running! 
	}
	if($init == 1){ #
		sleep 5;   # 5 more seconds to slide away,
		$init = 0; # somewhere in the sun... my head is in the sun...
	}
	$sats = "";
	my $satcount=0;
	my $saton=0; # satellites online
	## GET TPV Packet
	parsedat($gps->packet('TPV'));

	$lon = $jhash{'lon'};
	$alt = $jhash{'alt'};
	$alt.='m';
	$lat = $jhash{'lat'};
	my $cli;
	my $spe;
	if($jhash{'climb'}){ # sometimes doesn't exist
		if($jhash{'climb'} > 1){
			$cli = $jhash{'climb'};
		}else{
			$cli = 0.00;
		}
	}
	if($jhash{'speed'}){ # sometimes doesn't exist
		if($jhash{'speed'} > 1){
			$spe = $jhash{'speed'};
		}else{
			$spe = 0.00;
		}
	}
	my $tra = $jhash{'track'};
	parsedat($gps->packet('SKY')); ## GET SKY PACKET
	$satsq = ""; # reset satellite list to recreate:
	for(my $i=0;$i<=32;$i++){
		if($jhash{'satellites'}[$i]){ # does it exist?
			$satcount++; # total goes up (which, locally, is zero to start)
			my $satline .= "PRN ID:" . 
			$jhash{'satellites'}[$i]{'PRN'} . " AZ:" . 
			$jhash{'satellites'}[$i]{'az'} . " EL:" . 
			$jhash{'satellites'}[$i]{'el'} . " SS:" . 
			$jhash{'satellites'}[$i]{'ss'} . "dB ONLINE:" . 
			$jhash{'satellites'}[$i]{'used'} . "\n";
			$sats .= $satline; # I did this so I can use both :)
			if($jhash{'satellites'}[$i]{'used'} == 1){
				$saton++; # Online satellite caount goe up!
			}
			$satsq .= $jhash{'satellites'}[$i]{'PRN'} . ", ";
			if(!grep(/$jhash{'satellites'}[$i]{'PRN'}/,@allsats)){
				push(@allsats,$satline); # for logging ALL satellites for HTML
			}
		}else{ # we are done counting.
			$i=32;
		}
	}
	$cui->nostatus();
	if($lat && $lon){
		$lat =~ s/-//;
		$lon =~ s/-//;
		$lat.='N';
		$lon.='W';
	} # Here I update your window's labels with some data:
	$winlon::label->text($lon);
	$winlat::label->text($lat);
	$winalt::label->text($alt);
	$wincli::label->text($cli);
	$winspe::label->text($spe);
	$wintra::label->text($tra);
	$winsat::label->text($sats);
	$winson::label->text($saton);
	$winsc::label->text($satcount);
	if($saton >= 3){
		$winfix::progressbar->pos($satcount);
		$winfix::progressbar->set_color_fg("green");
		$widfix->title("3D Fix (clear skies)");
	}elsif($saton == 2){
		$winfix::progressbar->pos($satcount);
		$winfix::progressbar->set_color_fg("yellow");
		$widfix->title("2D Fix (long and lat only)");

	}else{
		$winfix::progressbar->pos($satcount);
		$winfix::progressbar->set_color_fg("red");
		$widfix->title("No Fix (No satellites online)");
		
	}
	getwifi(); # get some WiFi data
}
sub getwifi{ ### GET WIFI STATS ###
	$dump = "";
	# counting tokens:
	my ($wepc,$wpa2c,$wpac,$opnc) = 0;
	my ($ch1,$ch2,$ch3,$ch4,$ch5,$ch6,$ch7,$ch8,$ch9,$ch10,$ch11,$ch12,$ch13) = 0;
	my @aps = lib::WIFI->aps($filename);
	my %chan;
	my $apc = 0;
	foreach my $h (@aps){
		if($catch){ # check for Catchme-NG!
			if($h->{'ESSID'} =~ m/$catchme/){ # found it!
				catchme();
			}elsif($h->{'BSSID'} =~ m/$catchme/){ # found it!
				catchme();
			}
		}
		# create string for dump window:
		my $essid = $h->{'ESSID'};
		$essid =~ s/'/\'/g; # fix ESSIDs
		$dump .= $h->{'BSSID'} . ", " . $h->{'CH'} . ", " . $h->{'PWR'} . ", " . $h->{'SEC'} . ", " . $essid . "\n"; 
		if(!$lat){ $lat = "0.00000N"; }
		if(!$lon){ $lon = "0.00000W"; }
		if($lat ne "" && $lon ne "") { # let's not log if there's no coords so that JS doesn't break for the Maps:
			my $logaps = $h->{'BSSID'} . ", " . $h->{'CH'} . ", " . $h->{'PWR'} . ", " . $h->{'SEC'} . ", " . $essid . "," . $lat . "," . $lon;
			$logaps =~ s/, /,/g; # remove spaces from commas
			push(@logaps, $logaps) if (!grep(/$h->{'BSSID'}/,@logaps)); # push it into the array IF not existed
		}
		# lets count channels for pretty signal bars:
		$chan{$h->{'CH'}}++;
		$apc++;
		if($h->{'SEC'} =~ m/WEP/){
			$wepc++;
		}elsif($h->{'SEC'} =~ /WPA2/){
			$wpa2c++;
		}elsif($h->{'SEC'} =~ m/OPN/){
			$opnc++;
		}elsif($h->{'SEC'} =~ m/WPA /){
			$wpac++;
		}
	}
	$windmp::label->text($dump);
	if($chan{'13'}){
		sigbar($chan{'13'},$winch11::label);
	}if($chan{'12'}){
		sigbar($chan{'12'},$winch11::label);
	}if($chan{'11'}){
		sigbar($chan{'11'},$winch11::label);
	}if($chan{'10'}){
		sigbar($chan{'10'},$winch10::label);
	}if($chan{'9'}){
		sigbar($chan{'9'},$winch9::label);
	}if($chan{'8'}){
		sigbar($chan{'8'},$winch8::label);
	}if($chan{'7'}){
		sigbar($chan{'7'},$winch7::label);
	}if($chan{'6'}){
	sigbar($chan{'6'},$winch6::label);
	}if($chan{'5'}){
		sigbar($chan{'5'},$winch5::label);
	}if($chan{'4'}){
		sigbar($chan{'4'},$winch4::label);
	}if($chan{'3'}){
		sigbar($chan{'3'},$winch3::label);
	}if($chan{'2'}){
		sigbar($chan{'2'},$winch2::label);
	}if($chan{'1'}){
		sigbar($chan{'1'},$winch1::label);
	}
	$winwpa::label->text($wpac);
	$winwpa2::label->text($wpa2c);
	$winwep::label->text($wepc);
	$winopn::label->text($opnc);
	$winapc::label->text($apc);
	# Now draw our graph lines:
	graph($apc);
	getbt();
}
sub graph{ ### GRAPH CREATION ###
	$act++;
	if($act >= 117){ # reset the graph, it's full!
		clrgraph();
		$act=0;
	}
	my $total = 0;
	if($_[0] > 50){
		$total = 6;
	}elsif($_[0] >= 35 && $_[0] < 50){
		$total = 5;
	}elsif($_[0] >= 25 && $_[0] < 35){
		$total = 4;
	}elsif($_[0] >= 20 && $_[0] < 25){
		$total = 3;
	}elsif($_[0] >= 15 && $_[0] < 20){
		$total = 2;
	}elsif($_[0] >= 10 && $_[0] < 15){
		$total = 1;
	}elsif($_[0] >= 5 && $_[0] < 10){
		$total = 0;
	}
	for(my $i=($total+1);$i<=$#lines;$i++){
		$lines[$i] .= " "; # print spaces above peak on graph
	}
	if($total==0){ # add graph lines
		$lines[0] .= "|";
	}else{
		for(my $i=$total;$i>=0;$i--){
			$lines[$i] .= "|";
		}
	}
	my $buff = ""; # final print line
	for(my $i=6;$i>=0;$i--){ # print backwards:
	        $buff .= $lines[$i] . "\n";
	}

	# Now let's print the graph:
	$wingrph::label->text($buff);
}
sub clrgraph{ ## CLEAR THE GRAPH ##
	@lines = ("05- ","10  ","15  ","20  ","25  ","35  ","50+ ");
}
sub sigbar{ ### My Channel Signal Bars ###
	# bar is 10 height, so (10-$_[0]) == spacescount
	my $spaces = (9 - $_[0]);
	my $sigbar = '';
	my $n = 0;
	if($spaces > 0){ # 10 - 7 = 3
		for(my $i=0;$i<$spaces;$i++){
			$sigbar .= "\n"; # add spaces on top
		}
	}
	for(my $i=0;$i<$_[0];$i++){
		$sigbar .= "    \n"; # add signal on bottom
		$n++;
	}
	$_[1]->text($sigbar);
	$_[1]->reverse(1);
	# Now lets color it:
	if($n<3){ # color the bars for significance:
		$_[1]->set_color_fg('red');
	}elsif($n>=3 && $n < 7){
		$_[1]->set_color_fg('yellow');
	}elsif($n>=7 && $n < 10){
		$_[1]->set_color_fg('green');
	}elsif($n>=10 && $n < 15){
		$_[1]->set_color_fg('green');
	}elsif($n>=15 && $n<25){
		$_[1]->set_color_fg('blue');
	}
	elsif($n>=25){
		$_[1]->set_color_fg('white');
	}
	### UBERTOOTH SCAN ###
	if($ubton==1){ # scanning is on
		my @bars = lines2bars();
		my $n=0;
		@arrlabel=( # this is sad how if this is a "global" Perl cannot use it:
			$ubt2408::label,$ubt2409::label,$ubt2410::label,$ubt2411::label,$ubt2412::label,$ubt2413::label,$ubt2414::label,$ubt2415::label
			,$ubt2416::label,$ubt2417::label,$ubt2418::label,$ubt2419::label,$ubt2420::label,$ubt2421::label,
        		$ubt2422::label,$ubt2423::label,$ubt2424::label,$ubt2425::label,$ubt2426::label,$ubt2427::label,$ubt2428::label,
        		$ubt2429::label,$ubt2430::label,$ubt2431::label,$ubt2432::label,$ubt2433::label,$ubt2434::label,$ubt2435::label,
        		$ubt2436::label,$ubt2437::label,$ubt2438::label,$ubt2439::label,$ubt2440::label,$ubt2441::label,$ubt2442::label,
        		$ubt2443::label,$ubt2444::label,$ubt2445::label,$ubt2446::label,$ubt2447::label,$ubt2448::label,$ubt2449::label,
        		$ubt2450::label,$ubt2451::label,$ubt2452::label,$ubt2453::label,$ubt2454::label,$ubt2455::label,$ubt2456::label,
        		$ubt2457::label,$ubt2458::label,$ubt2459::label,$ubt2460::label,$ubt2461::label,$ubt2462::label,$ubt2463::label,
        		$ubt2464::label,$ubt2465::label,$ubt2466::label,$ubt2467::label,$ubt2468::label,$ubt2469::label,$ubt2470::label,
        		$ubt2471::label,$ubt2472::label);
=pod
,$ubt2473::label,$ubt2474::label,$ubt2475::label,$ubt2476::label,$ubt2477::label,
        		$ubt2478::label,$ubt2479::label,$ubt2480::label); # i'm looking forward to defining this fucking thing everywhere.
=cut
		foreach(@arrlabel){
			$_->text($bars[$n]);
			$n++;
			ubtcolor($_);
		}
	}
}
sub ubtcolor{
	my $str=$_[0]->get();
	my $cnt=()=$str =~ / /g;
	$_[0]->set_color_fg("red") if ($cnt<10);
	$_[0]->set_color_fg("orange") if ($cnt>=10 && $cnt<20);
	$_[0]->set_color_fg("yellow") if ($cnt>=20 && $cnt <=25);
	$_[0]->set_color_fg("green") if ($cnt > 25);
}
sub getbt{ ### Scan for all Bluetooth devices and 
	my @bt = `cat bt.out`; ### Pull out relevant data:
	$btdisp = ''; # what to display to the user:
	foreach(@bt){
		my @splt = split(/,/,$_);
		if(!grep(/$splt[0]/,@bttds)){ # if it's not in bluetooth devices,
			push(@bttds,$_);#  then push it in for logging
			$btdc++;
		}
		# either way, it can be displayed because it was found:
		$btdisp .= $_; # already has new-line
	}
	if($btdisp ne ''){ # then we have something to put in the window:
		$winbt::label->text($btdisp);
	}
}
sub parsedat{ ### Parse the satellite data ###
      	 my $json = JSON::XS->new->utf8->pretty->allow_nonref;
      	 %jhash = %{$json->decode ($_[0])};
}
sub eggsit{ ### QUIT CTRL+C "q" ###
	my $i = 0;
	my $quit = $cui->dialog(-message=>"Do you really want to exit?",-title=>"Thank You!",-buttons=>['yes','no'],-bg=>"black",-fg=>"white",-bold=>1);
	if($quit){
		stopubt(); # stop Ubertooth if quit from that screen.
		### SHALL WE LOG FILE? ###
		my $loga = $cui->dialog(-message=>"Shall I Map your Travel?",-title=>"Map Plotting",-buttons=>['yes','no'],-bold=>1,-bg=>"black",-fg=>"white");
		if($loga){
			my $date = localtime;
			$date =~ s/ //g;
			$date =~ s/:/-/g;
			open(FLE,">>logs/html/logfile-$date.html"); # TODO altitude in GMAPS ?
			printhtml('includes/html/top.html');
			my @slatlon = split(/,/,$logaps[0]); # get starting lat and lon
			my $slat = $slatlon[5];
			my $slon = $slatlon[6];
			my $map = lib::GMAPS->new($lat,$lon);
			print FLE "var lat = $slat;\n";
			print FLE "var lon = $slon;\n";
			printhtml('includes/html/middle.html');
			my $i = 0;
			foreach(@logaps){ # All of my WiFi APs:
				my @platlon = split(/,/,$_);# latitude and longitude for each marker object
				print FLE $map->pos_mark($i,$platlon[5],$platlon[6],'wf') . "\n"; # sent
				print FLE $map->con_str($platlon[0],$platlon[1],$platlon[2],$platlon[3],$platlon[4],$platlon[5],$platlon[6]) . "\n";
				print FLE $map->info_win($i) ."\n"; # make the info window for the marker
				$i++; # this little token keeps all map objects unique!
			} # $i is still at cap from wifi shit:
			foreach(@bttds){ # All of my Bluetooth Devices:
				my @line = split(/,/,$_);
				print FLE $map->pos_mark($i,$line[2],$line[3],'bt');
				print FLE $map->con_str_bt($line[0],$line[1],$line[2],$line[3]);
				print FLE $map->info_win($i);
				$i++;
			}
			foreach(@wps){ # All my Waypoints:
				my @line = split(/,/,$_);
				print FLE $map->pos_mark($i,$line[1],$line[0],'wp');
				print FLE $map->wp_str($line[0],$line[1],$line[2],$line[3],$line[4]);
				print FLE $map->info_win($i);
				$i++;
			}
			printhtml('includes/html/bottom.html');
			foreach(@logaps){ # Print the APs under the map:
				print FLE $_ . "<br />";
			}
			printhtml('includes/html/bottom_apdone.html');
			foreach(@bttds){ # all Bluetooth devices found - period.
				print FLE $_ . "<br />";
			}
			printhtml('includes/html/bottom_btdone.html');
			foreach(@allsats){
				print FLE $_ . "<br />";
			}
			printhtml('includes/html/bottom_satdone.html');
			
		}
		# Kill Airodump-ng:
		my @pid = `ps aux | grep "airodump.*capfiles" | awk '{print \$2}'`;
		foreach(@pid){
			system ("kill $_");
		}
		close FLE;
		exit(0);
	}
}
sub printhtml{ # slurp contents and print to file
	my @html = `cat $_[0]`;
	foreach(@html){
		print FLE $_;
	}
}
sub snapshot{ ### Take a snapshot of the surrounding area ###
	$date =~ s/ /./g;
	my $file = "logs/txt/wc." . $date . ".snapshot";
	open(FLE,">$file");
	print FLE "WARCARRIER Wardriving Location Snapshot\n -- taken " . 
		localtime() . "\n-----------------------------\nLongitude: " . 
		$lon . "\nLatitude: " . $lat . "\nAltitude: " . $alt . 
		"\n\nBSSIDs:\n--------------\n" . $dump . "\n\n" . 
		"Satellites:\n--------------\n". $sats . "\n". 
		"Bluetooth devices:\n--------------\n" . $btdisp . "\n";
	close FLE;
	my $statusstring="Snapshot successfully taken!\nFile: ./" . $file;
	$cui->error(-message=>$statusstring,-title=>"Snapshot of Surrounding Area");
}
sub satinfo{ ### WHich satellite to query? ###
	my $satquery = $cui->question(-question=>"Which satellite would you like more information on? Please choose PRNID:\n\n".$satsq,);
	satdat($satquery);
}
sub satdat{ ### Display the information ###
	my $sat = lib::SATINFO->new($_[0]);
	$cui->dialog(-height=>100,-width=>100,-message=>"PRNID:" . $sat->satdata('PRN') . "\nInternational Code: " . $sat->satdata('INTCODE') . "\nNORAD ID: " . $sat->satdata('NORADID') . "\nLaunch Date: " . $sat->satdata('LD') . "\nNAVSTAR ID: " . $sat->satdata('NAVSTAR') . "\n\n" . $sat->satdata('TRIV'),-title=>"Satellite Info from NASA",-tfg=>"black",-tbg=>"yellow");
}
### Catchme-NG STUFF ###
sub catchmeinput{
	$catchme = $cui->question(-title=>"Catchme-NG!", -question=>"Please enter an ESSID or BSSID:");
	$catch = 1;
}
sub catchme{
	$catch = 0;
	system ("play includes/sound/mp3.mp3 2>/dev/null &");
	$cui->error(-message=>"Writing report file: \n".$catchme ."-".localtime()."-report.txt",-title=>"Catchme-NG found a match!");
	my $file = "logs/txt/".$catchme . "-" . localtime() . "-report.txt";
	$file =~ s/ /./g;
	open(CME,">>$file");
	print CME "WARCARRIER / Catchme-NG! Report for string: " . $catchme . "\n--------------------------\n";
	print CME "Lat: " . $lat . "\n";
	print CME "Lon: " . $lon . "\n";
	print CME "Localtime: " . localtime() . "\n";
	close CME;
}
sub usage{
	if($_[0] eq 'dev'){
		print "Cannot find your device! ($wfdev)\nTry doing \"airstop-ng wlanX && airmon-ng start wlanX\"\n"
			. "where wlanX is your wireless device\n\n";
	}
	print "Usage: ./warcarrier -d <wlan device> -f <filename>\n";
	exit;
}
sub waypoint{
	my $msg = $cui->question(-title=>"WARCARRIER Waypoint Creation",-question=>"Please enter a description for this waypoint:");
	$wpc++;
	my $wp = $lon.",".$lat.",".$wpc.",".$date.",".$msg;
	push(@wps,$wp);
	$cui->status("Waypoint successfully created!");
	sleep 1; # so they can see the status above
}

### UBEROOTH SPEC ANALYZER STUFF: ###
sub startubt{ # start Ubertooth scanning:
	if($udisp==1){ 
		return; # it's already ON!
	}else{
		$udisp = 1; # turn on display for Uberooth Boolean (used for recording)
	}
	return if($ubton==1);
	system("./uber2th &");
	$winubt = $cui->add('ubt','Window',-y=>1,-title=>"Bluetooth Spectrum Analyzer",-border=>1,-height=>35,-width=>120,-padleft=>15,-tfg=>"black",-tbg=>"yellow");
	my $ldbm = $winubt->add('ldbm','Label',-height=>35,-width=>7,-fg=>"white",-text=>"-70dBm \n-\n--\n---\n--\n-75dBm\n-\n--\n---\n--\n-80dBm\n-\n--\n---\n--\n-85dBm\n-\n--\n---\n--\n-90dBm\n-\n--\n---\n--\n-95dBm\n-\n--\n---\n--\n-100dBm",-x=>0,-bold=>1);
	my $lghz = $winubt->add('lghz','Label',-height=>1,-width=>99,-fg=>"white",-text=>"2400MHz 2408MHz 2416MHz 2424MHz 2432MHz 2440MHz 2448MHz 2456MHz 2464MHz 2472MHz",-x=>8,-bold=>1,-y=>31);
	my $lclose = $winubt->add('lclose','Label',-dim=>1,-height=>1,-fg=>"white",-text=>"\t\t\t\t\t\t\t(r) start/stop recording site\t(x) to close",-x=>1,-padtop=>32,-width=>105);
	$lclose->set_attribute('-dim',1);
	$rec::label = $winubt->add('rec','Label',-fg=>"red",-width=>30,-x=>8,-text=>"",-padtop=>32,-bold=>1);
	## Build new labels: ##
	$ubt2408::label = $winubt->add('2408','Label',-height=>30,-width=>1,-reverse=>1,-fg=>"yellow",-y=>1,-x=>8);
	$ubt2409::label = $winubt->add('2409','Label',-height=>30,-width=>2,-reverse=>1,-fg=>"green",-y=>1,-x=>9);
	$ubt2410::label = $winubt->add('2410','Label',-height=>30,-width=>3,-reverse=>1,-fg=>"green",-y=>1,-x=>10);
	$ubt2411::label = $winubt->add('2411','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>11);
	$ubt2412::label = $winubt->add('2412','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>12);
	$ubt2413::label = $winubt->add('2413','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>13);
	$ubt2414::label = $winubt->add('2414','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>14);
	$ubt2415::label = $winubt->add('2415','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>15);
	$ubt2416::label = $winubt->add('2416','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>16);
	$ubt2417::label = $winubt->add('2417','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>17);
	$ubt2418::label = $winubt->add('2418','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>18);
	$ubt2419::label = $winubt->add('2419','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>19);
	$ubt2420::label = $winubt->add('2420','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>20);
	$ubt2421::label = $winubt->add('2421','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>21);
	$ubt2422::label = $winubt->add('2422','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>22);
	$ubt2423::label = $winubt->add('2423','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>23);
	$ubt2424::label = $winubt->add('2424','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>24);
	$ubt2425::label = $winubt->add('2425','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>25);
	$ubt2426::label = $winubt->add('2426','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>26);
	$ubt2427::label = $winubt->add('2427','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>27);
	$ubt2428::label = $winubt->add('2428','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>28);
	$ubt2429::label = $winubt->add('2429','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>29);
	$ubt2430::label = $winubt->add('2430','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>30);
	$ubt2431::label = $winubt->add('2431','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>31);
	$ubt2432::label = $winubt->add('2432','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>32);
	$ubt2433::label = $winubt->add('2433','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>33);
	$ubt2434::label = $winubt->add('2434','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>34);
	$ubt2435::label = $winubt->add('2435','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>35);
	$ubt2436::label = $winubt->add('2436','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>36);
	$ubt2437::label = $winubt->add('2437','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>37);
	$ubt2438::label = $winubt->add('2438','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>38);
	$ubt2439::label = $winubt->add('2439','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>39);
	$ubt2440::label = $winubt->add('2440','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>40);
	$ubt2441::label = $winubt->add('2441','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>41);
	$ubt2442::label = $winubt->add('2442','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>42);
	$ubt2443::label = $winubt->add('2443','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>43);
	$ubt2444::label = $winubt->add('2444','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>44);
	$ubt2445::label = $winubt->add('2445','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>45);
	$ubt2446::label = $winubt->add('2446','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>46);
	$ubt2447::label = $winubt->add('2447','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>47);
	$ubt2448::label = $winubt->add('2448','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>48);
	$ubt2449::label = $winubt->add('2449','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>49);
	$ubt2450::label = $winubt->add('2450','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>50);
	$ubt2451::label = $winubt->add('2451','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>51);
	$ubt2452::label = $winubt->add('2452','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>52);
	$ubt2453::label = $winubt->add('2453','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>53);
	$ubt2454::label = $winubt->add('2454','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>54);
	$ubt2455::label = $winubt->add('2455','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>55);
	$ubt2456::label = $winubt->add('2456','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>56);
	$ubt2457::label = $winubt->add('2457','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>57);
	$ubt2458::label = $winubt->add('2458','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>58);
	$ubt2459::label = $winubt->add('2459','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>59);
	$ubt2460::label = $winubt->add('2460','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>60);
	$ubt2461::label = $winubt->add('2461','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>61);
	$ubt2462::label = $winubt->add('2462','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>62);
	$ubt2463::label = $winubt->add('2463','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"green",-y=>1,-x=>63);
	$ubt2464::label = $winubt->add('2464','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>64);
	$ubt2465::label = $winubt->add('2465','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>65);
	$ubt2466::label = $winubt->add('2466','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>66);
	$ubt2467::label = $winubt->add('2467','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>67);
	$ubt2468::label = $winubt->add('2468','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>68);
	$ubt2469::label = $winubt->add('2469','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>69);
	$ubt2470::label = $winubt->add('2470','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>70);
	$ubt2471::label = $winubt->add('2471','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>71);
	$ubt2472::label = $winubt->add('2472','Label',-height=>30,-width=>4,-reverse=>1,-fg=>"yellow",-y=>1,-x=>72);
=pod	
	my $blue = $winubt->add('blue','Label',-width=>1,-height=>1,-reverse=>1,-fg=>"blue",-y=>1,-x=>83,-text=>" ");
	my $bluedbm = $winubt->add('bluedbm','Label',-width=>10,-height=>1,-fg=>"white",-y=>1,-x=>84,-text=>">-75dBm");
	my $green = $winubt->add('green','Label',-width=>1,-height=>1,-reverse=>1,-fg=>"green",-y=>2,-x=>83,-text=>" ");
	my $greendbm = $winubt->add('greendbm','Label',-width=>10,-height=>1,-fg=>"white",-y=>2,-x=>84,-text=>"-85~80dBm");
	my $yellow = $winubt->add('yellow','Label',-width=>1,-height=>1,-reverse=>1,-fg=>"yellow",-y=>3,-x=>83,-text=>" ");
	my $yellowdbm = $winubt->add('yellowdbm','Label',-width=>10,-height=>1,-fg=>"white",-y=>3,-x=>84,-text=>"-81~90dBm");
	my $red = $winubt->add('red','Label',-width=>1,-height=>1,-reverse=>1,-fg=>"red",-y=>4,-x=>83,-text=>" ");
	my $reddbm = $winubt->add('reddbm','Label',-width=>10,-height=>1,-fg=>"white",-y=>4,-x=>84,-text=>"<-91dBm");
=cut

=pod # unfortunately, Curses::UI is a big clunky piece of garbage. Deosn;t report errors, can't do globals, etc. 
	# this piece of code would have saved 80 lines, but cannot be implemented in Curses::UI's current "state"
	# not sure why the author didn;t give a fuck about errors. I mean, seriously.
	my $m=8;
	@arrlabel=($ubt2408::label,$ubt2409::label,$ubt2410::label,$ubt2411::label,$ubt2412::label,$ubt2413::label,$ubt2414::label,$ubt2415::label);
	foreach(@arrlabel){
		my $n = $m; # 2408
		$n += 2400;
		$_ = $winubt->add("$n","Label",-height=>30,-width=>4,-reverse=>1,-y=>1,-x=>$m,-border=>1);
		$m++;
	}
=cut		
	$ubton=1;
	$winubt->focus();
}
sub lines2bars{ # pass me an array plz
	if($record==1){ # show that we are recording
		$f++; # frames
		$rec::label->text("*Recording: ".$f." frames");
	}
	my @lines = `cat ubt.out`;
	if($record==1){ # we are recording, let's print the file:
		my $raw = `cat ubt.raw`;
		print UBT localtime().",".$lat.",".$lon.",".$raw; # nice.
	}
	my @lines2;
	my $str = ""; # initialize our string
	#for(my$i=0;$i<=length($lines[$#lines]);$i++){ # we strive until we reach NULL
	for(my$i=0;$i<=80;$i++){ # we strive until we reach NULL
		for(my$j=0;$j<=$#lines;$j++){
			my $s = substr($lines[$j],0,1); # get the element
			$lines[$j]=~s/^.//; # remove the element
			if($s =~ m/P/){
				$s =~ s/P/ \n/; # make it a printing space
			}else{
				$s = "\n"; # make it a non printing newline
			}
	                $str.=$s;
	        }
	        push(@lines2,$str); # something like "\n\n_\n_\n_\n"
		$str=""; # reset the string
	}
	return(@lines2); # done.
}
sub stopubt{ # stop ubertooth scanning:
	$udisp = 0; # turn off uberooth display
	$cui->delete('ubt');
	$ubton=0;
	my $killcmd = "ps aux | grep [s]pectool | awk '{print \$2}' | xargs kill 2>/dev/null";
	system($killcmd); # stop scanner
}
sub rec{
	if($record==0 && $udisp==1){ #if Ubertooth display is ON
		open(UBT,">logs/bluetooth/ubt.csv"); # this is where I store the log
		$record=1; # turn it on. turn it on and all the way up!
		# do record here $date.$lon.$lat.$btdump
	}else{
		close(UBT);
		$record=0; # turn it off, we are done recording
		$f=0; # frame count reset
		$rec::label->text(""); # just in case we stop when it says "*Recording*"
	}
}
